package com.cjjd.c.feature.mine.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarActivity;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.LoginBean;
import com.cjjd.c.bean.MobileCode;
import com.cjjd.c.bean.WxLoginAccessTokenBean;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.CountDownTimerUtils;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.StringUtil;
import com.cjjd.c.utils.ToastUtil;
import com.cjjd.c.view.MyEditText;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/9/26.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class WxLoginSetMobileActivity extends ActionBarActivity {
    @Bind(R.id.et_mobile)
    MyEditText etMobile;
    @Bind(R.id.mtv_captcha)
    MyEditText mtvCaptcha;
    @Bind(R.id.tv_get_captcha)
    TextView tvGetCaptcha;
    @Bind(R.id.tv_next)
    TextView tvNext;

    private String mobile;
    private String sessionId;
    private String captcha;
    private WxLoginAccessTokenBean wxLoginAccessTokenBean;

    @Override
    public Object getTitleString() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.activity_wx_login_set_mobile;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        processIntentData();

    }

    private void processIntentData() {
        wxLoginAccessTokenBean = (WxLoginAccessTokenBean) getIntent().getSerializableExtra("openId");
        if (wxLoginAccessTokenBean != null) {
            // 1. 发送手机短信验证码
            // 2. 验证短信验证码
            // 3. 绑定微信和手机号，并登录账号
        }
    }

    @OnClick({R.id.tv_get_captcha, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_captcha:
                if (checkMobile()) {
                    getMobileCodeForWeChat();
                }
                break;
            case R.id.tv_next:
                checkCaptcha();
                break;
        }
    }


    private void checkCaptcha() {

        mobile = etMobile.getText().toString().trim();
        captcha = mtvCaptcha.getText().toString().trim();

        if (TextUtils.isEmpty(captcha)) {
            ToastUtil.showLongToast(mContext, "请输入手机验证码！");
            return;
        }
        if (TextUtils.isEmpty(sessionId)) {
            ToastUtil.showToast("请重新获取验证码");
            return;
        }
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("mobile", mobile);
        h1.put("captcha", captcha);
        h1.put("sessionId", sessionId);

        Call<HttpResult<String>> validateMobileCodeForWeChat = service.validateMobileCodeForWeChat(JsonUtils.convertParam("validateMobileCodeForWeChat", h1));
        validateMobileCodeForWeChat.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                wechatBindAndLogin();
            }

        });

    }

    // 绑定微信id和手机号
    private void wechatBindAndLogin() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("mobile", mobile);
        h1.put("captcha", captcha);
        h1.put("sessionId", sessionId);
        h1.put("openid", wxLoginAccessTokenBean.openid);

        Call<HttpResult<LoginBean>> wechatBindAndLogin = service.wechatBindAndLogin(JsonUtils.convertParam("wechatBindAndLogin", h1));
        wechatBindAndLogin.enqueue(new CallbackImpl<LoginBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<LoginBean>> call, Response<HttpResult<LoginBean>> response) {
                sessionId = "";
                LoginBean loginBean = response.body().getData();
                SharePrefUtil.saveBoolean(mContext, ShareKeys.isLogin, true);
                SharePrefUtil.saveString(mContext, ShareKeys.Token, loginBean.token);
                SharePrefUtil.saveString(mContext, ShareKeys.sessionId, loginBean.sessionId);
                SharePrefUtil.saveString(mContext, ShareKeys.customerUuid, loginBean.customerUuid);
                EventBus.getDefault().post(EventBusKeys.login);
                finish();
            }

        });
    }


    private void getMobileCodeForWeChat() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("mobile", mobile);

        Call<HttpResult<MobileCode>> call = service.getMobileCodeForWeChat(JsonUtils.convertParam("getMobileCodeForWeChat", h1));
        call.enqueue(new CallbackImpl<MobileCode>() {
            @Override
            public void responseSuccess(Call<HttpResult<MobileCode>> call, Response<HttpResult<MobileCode>> response) {

                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvGetCaptcha, 60000, 1000);
                countDownTimerUtils.setMillisColor(getResources().getColor(R.color.white));
                countDownTimerUtils.start();

                MobileCode regMobileCode = response.body().getData();
                sessionId = regMobileCode.sessionId;
            }
        });
    }


    private boolean checkMobile() {
        mobile = etMobile.getText().toString().trim();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showLongToast(mContext, "请输入手机号码！");
            return false;
        }
        if (!StringUtil.isMobile(mobile)) {
            ToastUtil.showLongToast(mContext, "请输入正确的手机号码！");
            return false;
        }
        return true;
    }
}
